<?php
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Http_Client');
Zend_Loader::loadClass('Zend_Http_Client_Adapter_Socket');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
Zend_Loader::loadClass('Zend_Json');
Zend_Loader::loadClass('Zend_Gdata_App_CaptchaRequiredException');
require_once "Timer.php";

session_start();
$token = "CKaB74qzGhD0kqq-_f____8B";
$_SESSION['client'] = Zend_Gdata_Authsub::getHttpClient($token);
$client = $_SESSION['client'];
$json = new Zend_Json();
$type = $_POST['type'];
$eid = $_POST['eid'];
$sDate = $_POST['sDate'];
$eDate = $_POST['eDate'];
//Temporarily commented out until I figure out how
//to properly call events.phpA
//getAllEvents($client);
/*
if($type == "create"){

} else if($type == "update"){
	updateEvent ($client, $eid, $newTitle); 
}	else if($type == "delete"){
	deleteEvent($client, $eid);
}	else if ($type == "getall"){
	getAllEvents($client);
}
*/
/*
function deleteEvent ($client, $eventId){
        $gdataCal = new Zend_Gdata_Calendar($client);
	if ($eventDel = getEvent($client, $eventId)){
  	  echo "Removing: " . $eventDel->title->text . "<br />\n";
	  try {
	    $eventDel->delete();
	  } catch (Zend_Gdata_App_Exception $e) {
	    var_dump($e);
	    return null;
	  }
	  return 1;
	} else {
	  echo "Couldnt remove: " . $eventDel->title->text . "<br />\n";
	  return 0;
	}
}


function getAllEvents($client){
  $gdataCal = new Zend_Gdata_Calendar($client);
  $eventFeed = $gdataCal->getCalendarEventFeed();
  //echo "<ul>\n";
  $toreturn = array();
  $i=0;
  foreach ($eventFeed as $event) {
  	$toreturn[$i]=$event;
  	$i=$i+1;
//	echo "event $i ";
  }
 // echo "getAllEvents return<br>\n";
  return $toreturn;
  //return 0;
}
					    

function updateEvent ($client, $eventId, $newTitle){
    $gdataCal = new Zend_Gdata_Calendar($client);
    if ($eventOld = getEvent($client, $eventId)) {
      echo "Old title: " . $eventOld->title->text . "<br />\n";
      $eventOld->title = $gdataCal->newTitle($newTitle);
      try {
        $eventOld->save();
      } catch (Zend_Gdata_App_Exception $e) {
        var_dump($e);
        return null;
      }
      return 1;
    } else {
      return 0;
    }
}

function createEvent ($client, $title = '', $desc='', $where = '', $startDate = '', $startTime = '', $endDate = '', $endTime = '', $tzOffset = '-08'){
  $gdataCal = new Zend_Gdata_Calendar($client);
  $newEvent = $gdataCal->newEventEntry();
  
  $newEvent->title = $gdataCal->newTitle($title);
  $newEvent->where = array($gdataCal->newWhere($where));
  $newEvent->content = $gdataCal->newContent("$desc");
  
  $when = $gdataCal->newWhen();
  $when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
  $when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
  $newEvent->when = array($when);

  // Upload the event to the calendar server
  // A copy of the event as it is recorded on the server is returned
  $createdEvent = $gdataCal->insertEvent($newEvent);
  return $createdEvent;
}

	function getEvent($client, $eventId) 
	{ 
	  $gdataCal = new Zend_Gdata_Calendar($client); 
	  $query = $gdataCal->newEventQuery(); 
	  $query->setUser('default'); 
	  $query->setVisibility('private'); 
	  $query->setProjection('full'); 
	  $query->setEvent($eventId); 
	  try { 
	    $eventEntry = $gdataCal->getCalendarEventEntry($query); 
	    return $eventEntry; 
	  } catch (Zend_Gdata_App_Exception $e) { 
	    var_dump($e); 
	    return null; 
	  } 
	}
	
*/
/*
	date_parse returns an associative array like the following
	(
	    [year] => 2006
	    [month] => 12
	    [day] => 12
	    [hour] => 10
	    [minute] => 0
	    [second] => 0
	    [fraction] => 0.5
	    [warning_count] => 0
	    [warnings] => Array()
	    [error_count] => 0
	    [errors] => Array()
	    [is_localtime] => 
	)
*/
	function outputCalendarByDateRange($client, $startDate, $endDate){	
		$gdataCal = new Zend_Gdata_Calendar($client);
		$query = $gdataCal->newEventQuery();
		$query->setUser('default');
	 	$query->setVisibility('private');
		$query->setProjection('full');	      
		$query->setOrderby('starttime');
		$query->setSortOrder('ascending'); 
		$query->setStartMin($startDate);
		$query->setStartMax($endDate);
		$eventFeed = $gdataCal->getCalendarEventFeed($query);
		/*$toreturn=array();
		foreach ($eventFeed as $event) {
			$toreturn[]=$event;		
		}
	return $toreturn;*/
	return $eventFeed;
   }
//is there a way to tell what the interval is without passing it
	$startDate = strtotime("2008-04-25T8:00:00");
	$endDate = strtotime("2008-04-25T18:00");
	/*$timer = new Timer();
	$timer->start();*/
	$events = outputCalendarByDateRange($client, $startDate, $endDate); 
	/*$timer->stop();
	$timer->totaltime();*/
	$counter = $startDate;
	$interval = 3600;
	$rtn = "";
	foreach($events as $event){
		foreach($event->when as $when){//echo $when."<br/>";
			$d = split(" ", $when);
			$evStart = strtotime($d[1]);
			$evEnd = strtotime($d[3]);
			//echo "start: $evStart end: $evEnd<br/>";
			while($counter < $evStart && $evStart >= ($counter + $interval) && $counter < $evEnd){
				//echo "$counter<br/>";
				$counter += $interval;
				$rtn .= "00";
				//echo "$counter<br/>";
			}
			while($counter < $evEnd){
					$counter += $interval;
					$rtn .= "10";
			}
			
			//$rtn .= "|";
		}
	}
	while($counter < $endDate){
			$counter += $interval;
			$rtn .= "00";
		}	
	echo $rtn;
	
	if($rtn == "00001010000000101000")
		echo "correct";//should be "00000101000000010100"
	else
		echo "<br>try again";
?>